<template>
    <el-dialog
            :title="!dataForm.id ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
            <el-form-item label="头像" prop="userface">
<!--                <el-input v-model="dataForm.userface" placeholder=""></el-input>-->
                <el-upload
                        class="avatar-uploader"
                        :action="url"
                        :auto-upload="true"
                        :show-file-list="false"
                        :on-success="handleAvatarSuccess"
                        :before-upload="beforeAvatarUpload">
                    <img v-if="imageUrl" :src="imageUrl" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>
            <el-row>
                <el-col :span="8">
                    <el-form-item label="姓名" prop="name">
                        <el-input v-model="dataForm.name" placeholder="姓名"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="手机号码" prop="phone">
                        <el-input v-model="dataForm.phone" placeholder="手机号码"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="住宅电话" prop="telephone">
                        <el-input v-model="dataForm.telephone" placeholder="住宅电话"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="联系地址" prop="address">
                        <el-input v-model="dataForm.address" placeholder="联系地址"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="是否启用" prop="enabled">
                        <el-input v-model="dataForm.enabled" placeholder=""></el-input>
                        <el-switch
                                style="display: block"
                                v-model="dataForm.enabled"
                                active-color="#13ce66"
                                inactive-color="#ff4949"
                                active-text="启用"
                                inactive-text="禁用">
                        </el-switch>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="8">
                    <el-form-item label="用户名" prop="username">
                        <el-input v-model="dataForm.username" placeholder="用户名"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="密码" prop="password">
                        <el-input v-model="dataForm.password" placeholder="密码"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="角色" prop="selectrid">
                        <el-select v-model="dataForm.roleIds" multiple placeholder="请选择">
                            <el-option
                                    v-for="(r,indexR) in allRoles"
                                    :key="indexR"
                                    :label="r.nameZh"
                                    :value="r.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-form-item label="备注" prop="remark">
                <el-input v-model="dataForm.remark" placeholder=""></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
        name: "addOrUpdateHr",
        data () {
            return {
                url:'/system/hr/imgUpload',
                visible: false,
                imageUrl: '',
                allRoles:[],
                dataForm: {
                    id: null,
                    name: '',
                    phone: '',
                    telephone: '',
                    address: '',
                    enabled: true,
                    username: '',
                    password: '',
                    userface: '',
                    remark: '',
                    roleIds:[]
                },
                dataRule: {
                    name: [
                        { required: true, message: '姓名不能为空', trigger: 'blur' }
                    ],
                    phone: [
                        { required: true, message: '手机号码不能为空', trigger: 'blur' }
                    ],
                    telephone: [
                        { required: true, message: '住宅电话不能为空', trigger: 'blur' }
                    ],
                    address: [
                        { required: true, message: '联系地址不能为空', trigger: 'blur' }
                    ],
                    enabled: [
                        { required: true, message: '是否启用不能为空', trigger: 'blur' }
                    ],
                    username: [
                        { required: true, message: '用户名不能为空', trigger: 'blur' }
                    ],
                    // password: [
                    //     { required: true, message: '密码不能为空', trigger: 'blur' }
                    // ]
                }
            }
        },
        mounted() {
            this.initAllRole();
        },
        methods:{
            init (id) {
                this.initAllRole();
                this.dataForm.id = id
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (this.dataForm.id) {
                        var url ="/system/hr/getHrById"
                        this.postRequest(url,this.dataForm).then(res =>{
                            console.log(res);
                            if (res.status == 200) {
                                this.dataForm = {
                                    ...res.obj
                                }

                            }

                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit () {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        var url = "/system/hr/save"
                        if (this.dataForm.id) {
                            url = "/system/hr/update";
                        }
                        console.log(this.dataForm)
                        this.postRequest(url,this.dataForm).then(res =>{
                            console.log(res);
                            if (res.status == 200) {
                                this.visible = false
                                    this.$emit('refreshDataList')
                            } else {
                                this.$message.error(res.msg)
                            }

                        })
                    }
                })
            },
            handleAvatarSuccess(res, file) {
                this.imageUrl = URL.createObjectURL(file.raw);
                this.imageUrl = this.$target + res.obj.url;
                this.dataForm.userface = res.obj.url;
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },
            initAllRole() {
                this.getRequest("/system/hr/role").then(resp =>{
                    if (resp) {
                        this.allRoles = resp;
                    }
                })
            },
        }
    }
</script>

<style>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>
